<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <!-- <el-form-item label="仓库编号" prop="deptId">
        <el-input
          v-model="queryParams.deptId"
          placeholder="请输入仓库编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item> -->
      <el-form-item label="出库单号" prop="no">
        <el-input
          v-model="queryParams.no"
          placeholder="请输入出库单号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="核销报废类型" prop="type" label-width="100px">
        <el-select
          v-model="queryParams.type"
          placeholder="请选择核销报废类型"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.WRITEOFF_SCRAP_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="出库时间" prop="outTime">
        <el-date-picker
          v-model="queryParams.outTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item> -->
      <!-- <el-form-item label="合计数量" prop="totalCount">
        <el-input
          v-model="queryParams.totalCount"
          placeholder="请输入合计数量"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item> -->
      <!-- <el-form-item label="备注" prop="remark">
        <el-input
          v-model="queryParams.remark"
          placeholder="请输入备注"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item> -->
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择状态"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.WRITEOFF_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建者" prop="creator">
        <el-select
          v-model="queryParams.creator"
          clearable
          placeholder="请选择创建者"
          @keyup.enter="handleQuery"
          class="!w-240px"
        >
          <el-option
            v-for="item in userList"
            :key="item.id"
            :label="item.nickname"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['erp:check-out:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="序号" type="index" align="center" prop="id" width="60" />
      <!-- <el-table-column label="仓库编号" align="center" prop="deptId" /> -->
      <el-table-column label="出库单号" align="center" prop="no" />
      <el-table-column label="核销报废类型" align="center" prop="type">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.WRITEOFF_SCRAP_TYPE" :value="scope.row.type" />
        </template>
      </el-table-column>
      <!-- <el-table-column
        label="出库时间"
        align="center"
        prop="outTime"
        :formatter="dateFormatter"
        width="180px"
      /> -->
      <!-- <el-table-column label="合计数量" align="center" prop="totalCount" /> -->
      <!-- <el-table-column label="备注" align="center" prop="remark" /> -->
      <!-- <el-table-column label="附件 URL" align="center" prop="fileUrl" /> -->
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.WRITEOFF_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="创建者" align="center" prop="creator" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="操作" align="center" min-width="100px">
        
        
        <template #default="scope">
          <!-- 查看详情 -->
          <el-button
            link
            type="primary"
            @click="openDetail('update', scope.row.id)"
            v-hasPermi="['erp:check-out:query']"
          >
            详情
          </el-button>

        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>


  <!-- 详情弹窗 -->
  <CheckOutDetail ref="detailRef" @success="getList" />

</template>

<script setup lang="ts">
import { getIntDictOptions, getStrDictOptions, DICT_TYPE } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { CheckOutApi, CheckOutVO } from '@/api/erp/checkout'
// import CheckOutForm from './CheckOutForm.vue'
import CheckOutDetail from './CheckOutDetail.vue'
import { any, number } from 'vue-types'
import { getSimpleUserList,UserVO } from '@/api/system/user'

/** ERP 核销报废管理单 列表 */
defineOptions({ name: 'CheckOut' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<CheckOutVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const Info = JSON.parse(localStorage.getItem('user') || '{}');
const userInfo = JSON.parse(Info.v)
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  deptId: undefined,
  no: undefined,
  type: undefined,
  outTime: [],
  totalCount: undefined,
  remark: undefined,
  status: undefined,
  creator: undefined,
  createTime: [],
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中
const userList = ref<UserVO[]>([]) // 用户列表


/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    queryParams.flag = '0'
    const data = await CheckOutApi.getCheckOutPage(queryParams)
    // 过滤   要物资类型为1的数据
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await CheckOutApi.exportCheckOut(queryParams)
    download.excel(data, 'ERP 核销报废管理单.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

// 详情弹窗
const detailRef = ref()
const openDetail = (type: string, id: number) => {
  detailRef.value.open(type, id)
}



/** 初始化 **/
onMounted(async () => {
  getList()
  userList.value = await getSimpleUserList()
})
</script>